package day03;

import lombok.extern.slf4j.Slf4j;

/**
 * 面试题 两个线程先打印1后打印2 wait notify实现
 */
@Slf4j
public class test5 {
    private static Object lock = new Object();
    // 标识
    static boolean flag = false;
    public static void main(String[] args) {
        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                synchronized (lock){
                    while(!flag){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    log.debug("1");
                }
            }
        };

        Thread t2 = new Thread("t2") {
            @Override
            public void run() {
                synchronized (lock){
                    log.debug("2");
                    flag = true;
                    lock.notify();
                }
            }
        };

        t1.start();
        t2.start();
    }
}
